<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>mongodb常用命令操作 | East'blog</title><meta name="author" content="侯玉东"><meta name="copyright" content="侯玉东"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="mongodb常用命令操作">
<meta property="og:type" content="article">
<meta property="og:title" content="mongodb常用命令操作">
<meta property="og:url" content="https://houyudong1989.gitee.io/blog/2023/12/14/%E6%95%B0%E6%8D%AE%E5%BA%93/mongodb%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/index.html">
<meta property="og:site_name" content="East&#39;blog">
<meta property="og:description" content="mongodb常用命令操作">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://houyudong1989.gitee.io/blog/img/library1.jpeg">
<meta property="article:published_time" content="2023-12-14T00:54:17.732Z">
<meta property="article:modified_time" content="2023-12-14T00:55:21.356Z">
<meta property="article:author" content="侯玉东">
<meta property="article:tag" content="mongodb">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://houyudong1989.gitee.io/blog/img/library1.jpeg"><link rel="shortcut icon" href="/blog/img/head.jpg"><link rel="canonical" href="https://houyudong1989.gitee.io/blog/2023/12/14/%E6%95%B0%E6%8D%AE%E5%BA%93/mongodb%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'mongodb常用命令操作',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-12-14 08:55:21'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (true) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/blog/img/head.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">70</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友联</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg fixed" id="page-header" style="background-image: url('/blog/img/library6.jpeg')"><nav id="nav"><span id="blog-info"><a href="/blog/" title="East'blog"><img class="site-icon" src="/blog/img/head.jpg"/><span class="site-name">East'blog</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友联</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">mongodb常用命令操作</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-12-14T00:54:17.732Z" title="发表于 2023-12-14 08:54:17">2023-12-14</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-12-14T00:55:21.356Z" title="更新于 2023-12-14 08:55:21">2023-12-14</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/mongodb/">mongodb</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="mongodb常用命令操作"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="MongoDB常用命令操作"><a href="#MongoDB常用命令操作" class="headerlink" title="MongoDB常用命令操作"></a>MongoDB常用命令操作</h1><h2 id="进入mongodb"><a href="#进入mongodb" class="headerlink" title="进入mongodb"></a>进入mongodb</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--本地连接--&gt;</span></span><br><span class="line">mongo </span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--指定服务器连接--&gt;</span></span><br><span class="line">mongo <span class="comment">--host=centos135 --port=27017</span></span><br></pre></td></tr></table></figure>

<h2 id="创建数据库-、进入数据库"><a href="#创建数据库-、进入数据库" class="headerlink" title="创建数据库 、进入数据库"></a>创建数据库 、进入数据库</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--如果数据库存在，进入数据库，如果数据库不存在创建数据库--&gt;</span></span><br><span class="line">use mydb</span><br></pre></td></tr></table></figure>

<h2 id="删除数据库"><a href="#删除数据库" class="headerlink" title="删除数据库"></a>删除数据库</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.dropDatabase()</span><br></pre></td></tr></table></figure>

<h2 id="查看数据库"><a href="#查看数据库" class="headerlink" title="查看数据库"></a>查看数据库</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--查看所有持久化的数据库，刚创建的数据库没有数据存储在内存中，未持久化是看不见的--&gt;</span></span><br><span class="line"><span class="keyword">show</span> dbs</span><br><span class="line"><span class="keyword">show</span> databases</span><br></pre></td></tr></table></figure>

<h2 id="数据库表（集合）查看"><a href="#数据库表（集合）查看" class="headerlink" title="数据库表（集合）查看"></a>数据库表（集合）查看</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> tables</span><br></pre></td></tr></table></figure>

<h2 id="集合的显式创建"><a href="#集合的显式创建" class="headerlink" title="集合的显式创建"></a>集合的显式创建</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.createCollection(&quot;myTable&quot;)</span><br></pre></td></tr></table></figure>

<h2 id="集合的删除"><a href="#集合的删除" class="headerlink" title="集合的删除"></a>集合的删除</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.myTable.drop()</span><br></pre></td></tr></table></figure>

<h2 id="插入文档"><a href="#插入文档" class="headerlink" title="插入文档"></a>插入文档</h2><h3 id="插入单个文档"><a href="#插入单个文档" class="headerlink" title="插入单个文档"></a>插入单个文档</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.student.insertOne(&#123;&quot;student_id&quot;:&quot;001&quot;,&quot;name&quot;:&quot;xiaoming&quot;,&quot;age&quot;:<span class="number">12</span>,&quot;gender&quot;:<span class="number">1</span>,&quot;address&quot;:&quot;朝阳路12号&quot;,&quot;interest&quot;:&quot;看书&quot;&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="插入多个文档"><a href="#插入多个文档" class="headerlink" title="插入多个文档"></a>插入多个文档</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"> db.student.insertMany([</span><br><span class="line">&#123;&quot;_id&quot;:<span class="number">10</span>,&quot;name&quot;:&quot;xiaoqiang&quot;,&quot;gender&quot;:<span class="number">1</span>,&quot;age&quot;:<span class="number">16</span>,&quot;address&quot;:&quot;南京路12号&quot;,&quot;phone&quot;:[&quot;1576767676&quot;,&quot;0838-6438778&quot;]&#125;,</span><br><span class="line">&#123;&quot;_id&quot;:<span class="number">11</span>,&quot;name&quot;:&quot;xiaoqhui&quot;,&quot;gender&quot;:<span class="number">2</span>,&quot;age&quot;:<span class="number">13</span>,&quot;address&quot;:&quot;南京路123号&quot;,&quot;phone&quot;:[&quot;1576767676&quot;,&quot;0838-6438778&quot;]&#125;,</span><br><span class="line">&#123;&quot;_id&quot;:<span class="number">12</span>,&quot;name&quot;:&quot;xiaoming&quot;,&quot;gender&quot;:<span class="number">2</span>,&quot;age&quot;:<span class="number">16</span>,&quot;address&quot;:&quot;南京路212号&quot;,&quot;phone&quot;:[&quot;1576767676&quot;,&quot;0838-6438778&quot;]&#125;,</span><br><span class="line">&#123;&quot;_id&quot;:<span class="number">13</span>,&quot;name&quot;:&quot;xiaoqlan&quot;,&quot;gender&quot;:<span class="number">1</span>,&quot;age&quot;:<span class="number">14</span>,&quot;address&quot;:&quot;南京路122号&quot;,&quot;phone&quot;:[&quot;1576767676&quot;,&quot;0838-6438778&quot;]&#125;,</span><br><span class="line">&#123;&quot;_id&quot;:<span class="number">14</span>,&quot;name&quot;:&quot;xiaolv&quot;,&quot;gender&quot;:<span class="number">2</span>,&quot;age&quot;:<span class="number">18</span>,&quot;address&quot;:&quot;南京路122号&quot;,&quot;phone&quot;:[&quot;1576767676&quot;,&quot;0838-6438778&quot;]&#125;,</span><br><span class="line">&#123;&quot;_id&quot;:<span class="number">15</span>,&quot;name&quot;:&quot;xiaohong&quot;,&quot;gender&quot;:<span class="number">1</span>,&quot;age&quot;:<span class="number">19</span>,&quot;address&quot;:&quot;南京路112号&quot;,&quot;phone&quot;:[&quot;1576767676&quot;,&quot;0838-6438778&quot;]&#125;,</span><br><span class="line">&#123;&quot;_id&quot;:<span class="number">16</span>,&quot;name&quot;:&quot;xiaozi&quot;,&quot;gender&quot;:<span class="number">1</span>,&quot;age&quot;:<span class="number">11</span>,&quot;address&quot;:&quot;南京路112号&quot;,&quot;phone&quot;:[&quot;1576767676&quot;,&quot;0838-6438778&quot;]&#125;,</span><br><span class="line">&#123;&quot;_id&quot;:<span class="number">17</span>,&quot;name&quot;:&quot;xiaoliang&quot;,&quot;gender&quot;:<span class="number">2</span>,&quot;age&quot;:<span class="number">12</span>,&quot;address&quot;:&quot;南京路102号&quot;,&quot;phone&quot;:[&quot;1576767676&quot;,&quot;0838-6438778&quot;]&#125;,</span><br><span class="line">&#123;&quot;_id&quot;:<span class="number">18</span>,&quot;name&quot;:&quot;xiaocheng&quot;,&quot;gender&quot;:<span class="number">2</span>,&quot;age&quot;:<span class="number">10</span>,&quot;address&quot;:&quot;南京路162号&quot;,&quot;phone&quot;:[&quot;1576767676&quot;,&quot;0838-6438778&quot;]&#125;,</span><br><span class="line">&#123;&quot;_id&quot;:<span class="number">19</span>,&quot;name&quot;:&quot;xiaoqhuang&quot;,&quot;gender&quot;:<span class="number">2</span>,&quot;age&quot;:<span class="number">16</span>,&quot;address&quot;:&quot;南京路172号&quot;,&quot;phone&quot;:[&quot;1576767676&quot;,&quot;0838-6438778&quot;]&#125;,</span><br><span class="line">&#123;&quot;_id&quot;:<span class="number">20</span>,&quot;name&quot;:&quot;xiaoqing&quot;,&quot;gender&quot;:<span class="number">1</span>,&quot;age&quot;:<span class="number">15</span>,&quot;address&quot;:&quot;北京路122号&quot;,&quot;phone&quot;:[&quot;1576723432&quot;,&quot;0838-6546738&quot;]&#125;]);</span><br></pre></td></tr></table></figure>

<h2 id="删除文档"><a href="#删除文档" class="headerlink" title="删除文档"></a>删除文档</h2><h3 id="删除单个文档"><a href="#删除单个文档" class="headerlink" title="删除单个文档"></a>删除单个文档</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.student.deleteOne(&#123;&quot;student_id&quot;:&quot;001&quot;&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="批量删除文档"><a href="#批量删除文档" class="headerlink" title="批量删除文档"></a>批量删除文档</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.student.deleteMany(&#123;&quot;student_id&quot;:&quot;002&quot;&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="更新文档字段"><a href="#更新文档字段" class="headerlink" title="更新文档字段"></a>更新文档字段</h2><h3 id="更新单个文档"><a href="#更新单个文档" class="headerlink" title="更新单个文档"></a>更新单个文档</h3><h4 id="更新文档字段-1"><a href="#更新文档字段-1" class="headerlink" title="更新文档字段"></a>更新文档字段</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--如果更新的字段存在，就会更新字段的值，如果更新的字段不存在，就会创建这个字段--&gt;</span></span><br><span class="line"> db.student.updateOne(&#123;&quot;student_id&quot;:&quot;003&quot;&#125;,&#123;&quot;$set&quot;:&#123;&quot;name&quot;:&quot;xiaoliang&quot;&#125;&#125;)</span><br><span class="line"> <span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--可以同时更新多个字段--&gt;</span></span><br><span class="line">db.student.updateOne(&#123;&quot;student_id&quot;:&quot;005&quot;&#125;,&#123;&quot;$set&quot;:&#123;&quot;age&quot;:<span class="number">18</span>,&quot;phone&quot;:&quot;135263524536&quot;&#125;&#125;)</span><br></pre></td></tr></table></figure>

<h4 id="删除文档字段"><a href="#删除文档字段" class="headerlink" title="删除文档字段"></a>删除文档字段</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"> <span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--删除指定的字段,删除时也要保证k-v键值对的形式存在--&gt;</span></span><br><span class="line">db.student.updateOne(&#123;&quot;student_id&quot;:&quot;003&quot;&#125;,&#123;&quot;$unset&quot;:&#123;&quot;phone&quot;:&quot;13628376475&quot;&#125;&#125;)</span><br><span class="line">db.student.updateOne(&#123;&quot;student_id&quot;:&quot;003&quot;&#125;,&#123;&quot;$unset&quot;:&#123;&quot;name&quot;:&quot;&quot;&#125;&#125;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--错误演示--&gt;</span></span><br><span class="line">db.student.updateOne(&#123;&quot;student_id&quot;:&quot;003&quot;&#125;,&#123;&quot;$unset&quot;:&#123;&quot;name&quot;&#125;&#125;)</span><br></pre></td></tr></table></figure>

<h4 id="数值型字段递增和递减操作"><a href="#数值型字段递增和递减操作" class="headerlink" title="数值型字段递增和递减操作"></a>数值型字段递增和递减操作</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--递增操作--&gt;</span></span><br><span class="line">db.student.updateOne(&#123;&quot;student_id&quot;:&quot;003&quot;&#125;,&#123;&quot;$inc&quot;:&#123;&quot;age&quot;:<span class="number">1</span>&#125;&#125;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--递减操作--&gt;</span></span><br><span class="line">db.student.updateOne(&#123;&quot;student_id&quot;:&quot;003&quot;&#125;,&#123;&quot;$inc&quot;:&#123;&quot;age&quot;:<span class="number">-5</span>&#125;&#125;)</span><br></pre></td></tr></table></figure>

<h4 id="数组型字段"><a href="#数组型字段" class="headerlink" title="数组型字段"></a>数组型字段</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--$push用来插入数组，如果不存在就新增，如果存在，就在末尾插入数据，会将我们传递的类型当做元素插入，如果传递的是一个数组，那么元素就是数组元素，如果是字符串，元素类型就是字符串--&gt;</span></span><br><span class="line">db.student.updateOne(&#123;&quot;student_id&quot;:&quot;003&quot;&#125;,&#123;&quot;$push&quot;:&#123;&quot;phone&quot;:[&quot;010-888888&quot;,&quot;020-666666&quot;]&#125;&#125;)</span><br><span class="line">db.student.updateOne(&#123;&quot;student_id&quot;:&quot;003&quot;&#125;,&#123;&quot;$push&quot;:&#123;&quot;phone&quot;:[&quot;1388888888&quot;]&#125;&#125;)</span><br><span class="line">db.student.updateOne(&#123;&quot;student_id&quot;:&quot;003&quot;&#125;,&#123;&quot;$push&quot;:&#123;&quot;phone&quot;:&quot;1399999998&quot;&#125;&#125;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--使用$each配合$push可以同时向数组中插入多个元素--&gt;</span></span><br><span class="line">db.student.updateOne(&#123;&quot;name&quot;:&quot;xiaoqiang&quot;&#125;,&#123;&quot;$push&quot;:&#123;&quot;phone&quot;:&#123;&quot;$each&quot;:[&quot;123456443&quot;,&quot;14322222&quot;]&#125;&#125;&#125;);</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--使用$addToSet可以向数组中插入唯一元素，如果元素相同则不会添加,可以配合使用$each一次性插入多个元素 --&gt;</span></span><br><span class="line">db.student.updateOne(&#123;&quot;_id&quot;:<span class="number">11</span>&#125;,&#123;&quot;$addToSet&quot;:&#123;&quot;phone&quot;:&quot;13999999&quot;&#125;&#125;)</span><br><span class="line">db.student.updateOne(&#123;&quot;_id&quot;:<span class="number">11</span>&#125;,&#123;&quot;$addToSet&quot;:&#123;&quot;phone&quot;:&#123;&quot;$each&quot;:[&quot;1388888888&quot;,&quot;13999999&quot;]&#125;&#125;&#125;)</span><br></pre></td></tr></table></figure>

<h4 id="pull字段"><a href="#pull字段" class="headerlink" title="pull字段"></a>pull字段</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--使用$pull从数组中删除指定元素，如果数组中存在多个相同元素，会全部删掉</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"> db.student.updateOne(&#123;&quot;_id&quot;:<span class="number">1</span>&#125;,&#123;&quot;$pull&quot;:&#123;&quot;phone&quot;:&quot;1388888888&quot;&#125;&#125;)</span><br></pre></td></tr></table></figure>

<h4 id="pop字段"><a href="#pop字段" class="headerlink" title="pop字段"></a>pop字段</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--使用$pop可以从数组任意一端去删除元素,1代表从数组末尾删除一个元素，-1代表从数组头部删除一个元素。</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"> db.student.updateOne(&#123;&quot;_id&quot;:<span class="number">1</span>&#125;,&#123;&quot;$pop&quot;:&#123;&quot;phone&quot;:<span class="number">1</span>&#125;&#125;)</span><br><span class="line"> db.student.updateOne(&#123;&quot;_id&quot;:<span class="number">1</span>&#125;,&#123;&quot;$pop&quot;:&#123;&quot;phone&quot;:<span class="number">-1</span>&#125;&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="更新多个文档"><a href="#更新多个文档" class="headerlink" title="更新多个文档"></a>更新多个文档</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--批量更新字段--&gt;</span></span><br><span class="line">db.student.updateMany(&#123;&quot;student_id&quot;:&quot;005&quot;&#125;,&#123;&quot;$set&quot;:&#123;&quot;name&quot;:&quot;小白&quot;&#125;&#125;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--可以同时更新多个字段--&gt;</span></span><br><span class="line">db.student.updateMany(&#123;&quot;student_id&quot;:&quot;005&quot;&#125;,&#123;&quot;$set&quot;:&#123;&quot;age&quot;:<span class="number">18</span>,&quot;phone&quot;:&quot;135263524536&quot;&#125;&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="替换"><a href="#替换" class="headerlink" title="替换"></a>替换</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--使用replaceOne()替换集合中指定文档,替换文档内容，但是_id不变--&gt;</span><br><span class="line"> db.student.replaceOne(&#123;&quot;name&quot;:&quot;east&quot;&#125;,&#123;&quot;school&quot;:&quot;jialidun&quot;&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="集合数据查询"><a href="#集合数据查询" class="headerlink" title="集合数据查询"></a>集合数据查询</h2><h3 id="查询集合中的文档"><a href="#查询集合中的文档" class="headerlink" title="查询集合中的文档"></a>查询集合中的文档</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--查询所有集合中的文档--&gt;</span></span><br><span class="line">  db.student.find()</span><br><span class="line">  <span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--根据指定条件查询数据--&gt;</span></span><br><span class="line">  db.student.find(&#123;&quot;student_id&quot;:&quot;007&quot;&#125;)</span><br><span class="line">  <span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--多条件查询，同时满足两个条件的数据会被查询出来--&gt;</span></span><br><span class="line">  db.student.find(&#123;&quot;student_id&quot;:&quot;007&quot;,&quot;age&quot;:<span class="number">17</span>&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="查询指定字段数据"><a href="#查询指定字段数据" class="headerlink" title="查询指定字段数据"></a>查询指定字段数据</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--通过指定要显示的字段名称来过滤要显示的字段，如果字段设置为1，则显示该字段，设置为0则隐藏字段。_id默认是显示的，如果要不显示_id，则需要设置_id为0--&gt;</span></span><br><span class="line"> db.student.find(&#123;&quot;student_id&quot;:&quot;007&quot;&#125;,&#123;&quot;student_id&quot;:<span class="number">1</span>,&quot;name&quot;:<span class="number">1</span>,&quot;address&quot;:<span class="number">1</span>&#125;)</span><br><span class="line"> db.student.find(&#123;&quot;student_id&quot;:&quot;007&quot;&#125;,&#123;&quot;student_id&quot;:<span class="number">1</span>,&quot;name&quot;:<span class="number">1</span>,&quot;address&quot;:<span class="number">1</span>,&quot;_id&quot;:<span class="number">0</span>&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="查询单条记录"><a href="#查询单条记录" class="headerlink" title="查询单条记录"></a>查询单条记录</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--查询单条文档，没有参数默认是集合的第一条--&gt;</span></span><br><span class="line">db.student.findOne()</span><br></pre></td></tr></table></figure>

<h3 id="范围查询"><a href="#范围查询" class="headerlink" title="范围查询"></a>范围查询</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--查询年龄小于20岁的所有数据--&gt;</span></span><br><span class="line"> db.student.find(&#123;&quot;age&quot;:&#123;&quot;$lt&quot;:<span class="number">20</span>&#125;&#125;)</span><br><span class="line"> <span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--查询年龄大于20岁的所有数据--&gt;</span></span><br><span class="line">db.student.find(&#123;&quot;age&quot;:&#123;&quot;$gt&quot;:<span class="number">20</span>&#125;&#125;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--查询年龄小于等于17岁的所有数据--&gt;</span></span><br><span class="line">db.student.find(&#123;&quot;age&quot;:&#123;&quot;$lte&quot;:<span class="number">17</span>&#125;&#125;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--查询年龄大于等于32岁的所有数据--&gt;</span></span><br><span class="line">db.student.find(&#123;&quot;age&quot;:&#123;&quot;$gte&quot;:<span class="number">32</span>&#125;&#125;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--查询年龄大于12并且小于17岁的所有数据--&gt;</span></span><br><span class="line">db.student.find(&#123;&quot;age&quot;:&#123;&quot;$lt&quot;:<span class="number">12</span>,&quot;$gt&quot;:<span class="number">17</span>&#125;&#125;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--查询某个字段满足多个条件的结果，查询年龄是15岁和32岁的所有数据--&gt;</span></span><br><span class="line">db.student.find(&#123;&quot;age&quot;:&#123;&quot;$in&quot;:[<span class="number">32</span>,<span class="number">15</span>]&#125;&#125;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--查询student_id等于005或者gender等于2的所有数据--&gt;</span></span><br><span class="line">db.student.find(&#123;&quot;$or&quot;:[&#123;&quot;student_id&quot;:&quot;005&quot;&#125;,&#123;&quot;gender&quot;:<span class="number">2</span>&#125;]&#125;)</span><br><span class="line"> <span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--查询年龄不是13,16,19的索引数据--&gt;</span></span><br><span class="line">db.student.find(&#123;&quot;age&quot;:&#123;&quot;$nin&quot;:[<span class="number">13</span>,<span class="number">16</span>,<span class="number">19</span>]&#125;&#125;)</span><br><span class="line"> <span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--查询student_id等于005并且年龄大于14岁的所有数据--&gt;</span></span><br><span class="line">db.student.find(&#123;&quot;student_id&quot;:&quot;005&quot;,&quot;age&quot;:&#123;&quot;$gt&quot;:<span class="number">14</span>&#125;&#125;)</span><br><span class="line"> <span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--年龄除以3，得到的余数如果等于1，则匹配成功--&gt;</span></span><br><span class="line">db.student.find(&#123;&quot;age&quot;:&#123;&quot;$mod&quot;:[<span class="number">3</span>,<span class="number">1</span>]&#125;&#125;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--对&#123;&quot;$mod&quot;:[3,1]&#125;&#125;结果取反--&gt;</span></span><br><span class="line">db.student.find(&#123;&quot;age&quot;:&#123;&quot;$not&quot;:&#123;&quot;$mod&quot;:[<span class="number">3</span>,<span class="number">1</span>]&#125;&#125;&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="统计"><a href="#统计" class="headerlink" title="统计"></a>统计</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--统计student表所有数据的数量--&gt;</span></span><br><span class="line">db.student.<span class="built_in">count</span>()</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--统计年龄为18岁的数量--&gt;</span></span><br><span class="line">db.student.<span class="built_in">count</span>(&#123;&quot;age&quot;:<span class="number">18</span>&#125;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--统计年龄大于17并且小于40岁的人数--&gt;</span></span><br><span class="line">db.student.<span class="built_in">count</span>(&#123;&quot;age&quot;:&#123;&quot;$gt&quot;:<span class="number">17</span>,&quot;$lt&quot;:<span class="number">40</span>&#125;&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="分页"><a href="#分页" class="headerlink" title="分页"></a>分页</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--演练数据--&gt;</span></span><br><span class="line">db.page.insertMany([&#123;&quot;page&quot;:<span class="number">1</span>,&quot;title&quot;:&quot;第一页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">2</span>,&quot;title&quot;:&quot;第2页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">3</span>,&quot;title&quot;:&quot;第3页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">4</span>,&quot;title&quot;:&quot;第4页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">5</span>,&quot;title&quot;:&quot;第5页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">6</span>,&quot;title&quot;:&quot;第6页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">7</span>,&quot;title&quot;:&quot;第7页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">8</span>,&quot;title&quot;:&quot;第8页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">9</span>,&quot;title&quot;:&quot;第9页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">10</span>,&quot;title&quot;:&quot;第10页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">11</span>,&quot;title&quot;:&quot;第1页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">12</span>,&quot;title&quot;:&quot;第12页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">13</span>,&quot;title&quot;:&quot;第13页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">14</span>,&quot;title&quot;:&quot;第14页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">15</span>,&quot;title&quot;:&quot;第15页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">16</span>,&quot;title&quot;:&quot;第16页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">17</span>,&quot;title&quot;:&quot;第17页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">18</span>,&quot;title&quot;:&quot;第18页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">19</span>,&quot;title&quot;:&quot;第19页&quot;&#125;,&#123;&quot;page&quot;:<span class="number">20</span>,&quot;title&quot;:&quot;第20页&quot;&#125;])</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--忽略3条数据，从最开始处忽略--&gt;</span></span><br><span class="line">db.page.find().<span class="keyword">skip</span>(<span class="number">3</span>)</span><br><span class="line"> <span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--读取指定数量的数据--&gt;</span></span><br><span class="line">db.page.find().limit(<span class="number">3</span>)</span><br><span class="line"> <span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--利用skip和limit的结合，可以实现分页效果，limit决定每一页显示的数量，skip决定显示那些数据。</span></span><br><span class="line">如果每页显示的数量为 num，那么<span class="keyword">skip</span>的值为 num <span class="operator">*</span> （n<span class="number">-1</span>），n代表第几页。比如说，</span><br><span class="line"> 每一页显示<span class="number">5</span>条数据，num<span class="operator">=</span><span class="number">5</span>，</span><br><span class="line"> 然后第一页<span class="keyword">skip</span>值<span class="operator">=</span><span class="number">5</span><span class="operator">*</span>（<span class="number">1</span><span class="number">-1</span>）<span class="operator">=</span><span class="number">0</span>，</span><br><span class="line"> 第二页<span class="keyword">skip</span>的值<span class="operator">=</span><span class="number">5</span><span class="operator">*</span>（<span class="number">2</span><span class="number">-1</span>）<span class="operator">=</span><span class="number">5</span></span><br><span class="line"> <span class="comment">--&gt;</span></span><br><span class="line"> db.page.find().<span class="keyword">skip</span>(<span class="number">0</span>).limit(<span class="number">5</span>)</span><br><span class="line"> db.page.find().<span class="keyword">skip</span>(<span class="number">5</span>).limit(<span class="number">5</span>)</span><br><span class="line"> db.page.find().<span class="keyword">skip</span>(<span class="number">10</span>).limit(<span class="number">5</span>)</span><br></pre></td></tr></table></figure>

<h2 id="正则结果匹配"><a href="#正则结果匹配" class="headerlink" title="正则结果匹配"></a>正则结果匹配</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--匹配姓名以g结尾的数据--&gt;</span></span><br><span class="line"> db.student.find(&#123;&quot;name&quot;:<span class="operator">/</span>g$<span class="operator">/</span>&#125;)</span><br><span class="line"> <span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--匹配姓名以x开头的数据--&gt;</span></span><br><span class="line"> db.student.find(&#123;&quot;name&quot;:<span class="operator">/</span><span class="operator">^</span>x<span class="operator">/</span>&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="排序"><a href="#排序" class="headerlink" title="排序"></a>排序</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--对查询结果按年龄升序进行排序--&gt;</span></span><br><span class="line">db.student.find().sort(&#123;&quot;age&quot;:<span class="number">1</span>&#125;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--对查询结果按照年龄降序进行排序--&gt;</span></span><br><span class="line">db.student.find().sort(&#123;&quot;age&quot;:<span class="number">-1</span>&#125;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--多条件排序，先满足第一条件在满足第二条件--&gt;</span></span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--先将student_id进行升序排列，然后对每一个student_id里面的年龄进行降序排列--&gt;</span></span><br><span class="line">db.student.find().sort(&#123;&quot;student_id&quot;:<span class="number">1</span>,&quot;age&quot;:<span class="number">-1</span>&#125;)</span><br></pre></td></tr></table></figure>

<h1 id="执行规则"><a href="#执行规则" class="headerlink" title="执行规则"></a>执行规则</h1><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--skip、limit、sort在执行的时候，他们的执行顺序是固定的，先执行sort，然后执行skip，最后执行limit。跟我们编写命令的顺序无关。--&gt;</span></span><br><span class="line"></span><br><span class="line"> db.page.find().limit(<span class="number">5</span>).<span class="keyword">skip</span>(<span class="number">5</span>).sort(&#123;&quot;page&quot;:<span class="number">-1</span>&#125;)</span><br><span class="line"> db.page.find().<span class="keyword">skip</span>(<span class="number">5</span>).sort(&#123;&quot;page&quot;:<span class="number">-1</span>&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="索引"><a href="#索引" class="headerlink" title="索引"></a>索引</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--获取student集合的索引--&gt;</span></span><br><span class="line">db.student.getIndexes()</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--删除student集合名为name_1的索引--&gt;</span></span><br><span class="line">db.student.dropIndex(&quot;name_1&quot;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--给student集合的name字段创建索引--&gt;</span></span><br><span class="line">db.student.createIndex(&#123;&quot;name&quot;:<span class="number">1</span>&#125;)</span><br></pre></td></tr></table></figure>

<h1 id="聚合"><a href="#聚合" class="headerlink" title="聚合"></a>聚合</h1><h2 id="聚合管道"><a href="#聚合管道" class="headerlink" title="聚合管道"></a>聚合管道</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--修改输出文档结构，类似于select _id,student_id,name from student--&gt;</span></span><br><span class="line"> db.student.aggregate(&#123;&quot;$project&quot;:&#123;&quot;_id&quot;:<span class="number">0</span>,&quot;student_id&quot;:<span class="number">1</span>,&quot;name&quot;:<span class="number">1</span>&#125;&#125;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--管道过滤：过滤所有学生id为004的数据--&gt;</span></span><br><span class="line"> db.student.aggregate(&#123;&quot;$match&quot;:&#123;&quot;student_id&quot;:&quot;004&quot;&#125;&#125;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--分别统计男生、女生年龄之和（_id不能变，只能修改_id的值）--&gt;</span></span><br><span class="line"> db.student.aggregate(&#123;&quot;$group&quot;:&#123;&quot;_id&quot;:&quot;$gender&quot;,&quot;total&quot;:&#123;&quot;$sum&quot;:&quot;$age&quot;&#125;&#125;&#125;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--分别统计男生平均年龄（_id不能变，只能修改_id的值）--&gt;</span></span><br><span class="line"> db.student.aggregate([&#123;&quot;$match&quot;:&#123;&quot;gender&quot;:<span class="number">2</span>&#125;&#125;,&#123;&quot;$group&quot;:&#123;&quot;_id&quot;:&quot;$gender&quot;,&quot;total&quot;:&#123;&quot;$avg&quot;:&quot;$age&quot;&#125;&#125;&#125;   ])</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="mapReduce"><a href="#mapReduce" class="headerlink" title="mapReduce"></a>mapReduce</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">db.student.mapReduce( </span><br><span class="line"><span class="keyword">function</span>()&#123;emit(this.student_id,this.age)&#125;, </span><br><span class="line"><span class="keyword">function</span>(key,<span class="keyword">values</span>)&#123;<span class="keyword">return</span> Array.<span class="built_in">sum</span>(<span class="keyword">values</span>)&#125;, </span><br><span class="line">&#123;   query:&#123;&quot;gender&quot;:<span class="number">2</span>&#125;,</span><br><span class="line">    <span class="keyword">out</span>:&quot;results&quot;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="operator">&lt;</span><span class="operator">!</span><span class="comment">--查询所有女性数据，同时指定student_id进行分组，计算所有student_id相同数据的年龄之和--&gt;</span></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://houyudong1989.gitee.io/blog">侯玉东</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://houyudong1989.gitee.io/blog/2023/12/14/%E6%95%B0%E6%8D%AE%E5%BA%93/mongodb%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/">https://houyudong1989.gitee.io/blog/2023/12/14/%E6%95%B0%E6%8D%AE%E5%BA%93/mongodb%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://houyudong1989.gitee.io/blog" target="_blank">East'blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/mongodb/">mongodb</a></div><div class="post_share"><div class="social-share" data-image="/blog/img/library1.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2023/12/15/harmonyOS/Canvas%E7%BB%98%E5%88%B6%E6%8A%98%E7%BA%BF%E5%9B%BE/" title="canvas绘制折线图"><img class="cover" src="/blog/img/library1.jpeg" onerror="onerror=null;src='/blog/img/friend_404.gif'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">canvas绘制折线图</div></div></a></div><div class="next-post pull-right"><a href="/blog/2023/12/13/c++/%E5%BC%80%E5%8F%91%E8%BF%87%E7%A8%8B%E4%B8%AD%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98/cmake%E7%BC%96%E8%AF%91%E5%A4%B1%E8%B4%A5/" title="cmake编译失败"><img class="cover" src="/blog/img/library1.jpeg" onerror="onerror=null;src='/blog/img/friend_404.gif'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">cmake编译失败</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/blog/img/head.jpg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">侯玉东</div><div class="author-info__description">生命不息，奋斗不止</div></div><div class="card-info-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">70</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/houyudong1989"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/houyudong1989" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:420098321@qq.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">欢迎来到我的博客！！！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#MongoDB%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4%E6%93%8D%E4%BD%9C"><span class="toc-number">1.</span> <span class="toc-text">MongoDB常用命令操作</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%9B%E5%85%A5mongodb"><span class="toc-number">1.1.</span> <span class="toc-text">进入mongodb</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E6%95%B0%E6%8D%AE%E5%BA%93-%E3%80%81%E8%BF%9B%E5%85%A5%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-number">1.2.</span> <span class="toc-text">创建数据库 、进入数据库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-number">1.3.</span> <span class="toc-text">删除数据库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-number">1.4.</span> <span class="toc-text">查看数据库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%BA%93%E8%A1%A8%EF%BC%88%E9%9B%86%E5%90%88%EF%BC%89%E6%9F%A5%E7%9C%8B"><span class="toc-number">1.5.</span> <span class="toc-text">数据库表（集合）查看</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%9B%86%E5%90%88%E7%9A%84%E6%98%BE%E5%BC%8F%E5%88%9B%E5%BB%BA"><span class="toc-number">1.6.</span> <span class="toc-text">集合的显式创建</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%9B%86%E5%90%88%E7%9A%84%E5%88%A0%E9%99%A4"><span class="toc-number">1.7.</span> <span class="toc-text">集合的删除</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8F%92%E5%85%A5%E6%96%87%E6%A1%A3"><span class="toc-number">1.8.</span> <span class="toc-text">插入文档</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8F%92%E5%85%A5%E5%8D%95%E4%B8%AA%E6%96%87%E6%A1%A3"><span class="toc-number">1.8.1.</span> <span class="toc-text">插入单个文档</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8F%92%E5%85%A5%E5%A4%9A%E4%B8%AA%E6%96%87%E6%A1%A3"><span class="toc-number">1.8.2.</span> <span class="toc-text">插入多个文档</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E6%96%87%E6%A1%A3"><span class="toc-number">1.9.</span> <span class="toc-text">删除文档</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E5%8D%95%E4%B8%AA%E6%96%87%E6%A1%A3"><span class="toc-number">1.9.1.</span> <span class="toc-text">删除单个文档</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%89%B9%E9%87%8F%E5%88%A0%E9%99%A4%E6%96%87%E6%A1%A3"><span class="toc-number">1.9.2.</span> <span class="toc-text">批量删除文档</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9B%B4%E6%96%B0%E6%96%87%E6%A1%A3%E5%AD%97%E6%AE%B5"><span class="toc-number">1.10.</span> <span class="toc-text">更新文档字段</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9B%B4%E6%96%B0%E5%8D%95%E4%B8%AA%E6%96%87%E6%A1%A3"><span class="toc-number">1.10.1.</span> <span class="toc-text">更新单个文档</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9B%B4%E6%96%B0%E6%96%87%E6%A1%A3%E5%AD%97%E6%AE%B5-1"><span class="toc-number">1.10.1.1.</span> <span class="toc-text">更新文档字段</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E6%96%87%E6%A1%A3%E5%AD%97%E6%AE%B5"><span class="toc-number">1.10.1.2.</span> <span class="toc-text">删除文档字段</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E5%80%BC%E5%9E%8B%E5%AD%97%E6%AE%B5%E9%80%92%E5%A2%9E%E5%92%8C%E9%80%92%E5%87%8F%E6%93%8D%E4%BD%9C"><span class="toc-number">1.10.1.3.</span> <span class="toc-text">数值型字段递增和递减操作</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E5%9E%8B%E5%AD%97%E6%AE%B5"><span class="toc-number">1.10.1.4.</span> <span class="toc-text">数组型字段</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#pull%E5%AD%97%E6%AE%B5"><span class="toc-number">1.10.1.5.</span> <span class="toc-text">pull字段</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#pop%E5%AD%97%E6%AE%B5"><span class="toc-number">1.10.1.6.</span> <span class="toc-text">pop字段</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9B%B4%E6%96%B0%E5%A4%9A%E4%B8%AA%E6%96%87%E6%A1%A3"><span class="toc-number">1.10.2.</span> <span class="toc-text">更新多个文档</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9B%BF%E6%8D%A2"><span class="toc-number">1.11.</span> <span class="toc-text">替换</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%9B%86%E5%90%88%E6%95%B0%E6%8D%AE%E6%9F%A5%E8%AF%A2"><span class="toc-number">1.12.</span> <span class="toc-text">集合数据查询</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E8%AF%A2%E9%9B%86%E5%90%88%E4%B8%AD%E7%9A%84%E6%96%87%E6%A1%A3"><span class="toc-number">1.12.1.</span> <span class="toc-text">查询集合中的文档</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E8%AF%A2%E6%8C%87%E5%AE%9A%E5%AD%97%E6%AE%B5%E6%95%B0%E6%8D%AE"><span class="toc-number">1.12.2.</span> <span class="toc-text">查询指定字段数据</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E8%AF%A2%E5%8D%95%E6%9D%A1%E8%AE%B0%E5%BD%95"><span class="toc-number">1.12.3.</span> <span class="toc-text">查询单条记录</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8C%83%E5%9B%B4%E6%9F%A5%E8%AF%A2"><span class="toc-number">1.12.4.</span> <span class="toc-text">范围查询</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%9F%E8%AE%A1"><span class="toc-number">1.13.</span> <span class="toc-text">统计</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%86%E9%A1%B5"><span class="toc-number">1.14.</span> <span class="toc-text">分页</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%AD%A3%E5%88%99%E7%BB%93%E6%9E%9C%E5%8C%B9%E9%85%8D"><span class="toc-number">1.15.</span> <span class="toc-text">正则结果匹配</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8E%92%E5%BA%8F"><span class="toc-number">1.16.</span> <span class="toc-text">排序</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%89%A7%E8%A1%8C%E8%A7%84%E5%88%99"><span class="toc-number">2.</span> <span class="toc-text">执行规则</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B4%A2%E5%BC%95"><span class="toc-number">2.1.</span> <span class="toc-text">索引</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E8%81%9A%E5%90%88"><span class="toc-number">3.</span> <span class="toc-text">聚合</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%81%9A%E5%90%88%E7%AE%A1%E9%81%93"><span class="toc-number">3.1.</span> <span class="toc-text">聚合管道</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#mapReduce"><span class="toc-number">3.2.</span> <span class="toc-text">mapReduce</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/05/06/c++/5%E3%80%81%E5%AD%97%E7%AC%A6%E5%92%8C%E5%AD%97%E7%AC%A6%E4%B8%B2/" title="字符和字符串"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="字符和字符串"/></a><div class="content"><a class="title" href="/blog/2024/05/06/c++/5%E3%80%81%E5%AD%97%E7%AC%A6%E5%92%8C%E5%AD%97%E7%AC%A6%E4%B8%B2/" title="字符和字符串">字符和字符串</a><time datetime="2024-05-06T11:15:27.280Z" title="发表于 2024-05-06 19:15:27">2024-05-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/05/06/c++/4%E3%80%81%E6%8C%87%E9%92%88%E5%92%8C%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88/" title="指针和函数指针"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="指针和函数指针"/></a><div class="content"><a class="title" href="/blog/2024/05/06/c++/4%E3%80%81%E6%8C%87%E9%92%88%E5%92%8C%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88/" title="指针和函数指针">指针和函数指针</a><time datetime="2024-05-06T11:14:29.047Z" title="发表于 2024-05-06 19:14:29">2024-05-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/05/05/python/7%E3%80%81m3u8%E8%A7%86%E9%A2%91%E6%A0%BC%E5%BC%8F/" title="m3u8格式"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="m3u8格式"/></a><div class="content"><a class="title" href="/blog/2024/05/05/python/7%E3%80%81m3u8%E8%A7%86%E9%A2%91%E6%A0%BC%E5%BC%8F/" title="m3u8格式">m3u8格式</a><time datetime="2024-05-05T01:43:26.865Z" title="发表于 2024-05-05 09:43:26">2024-05-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/04/30/android/charles%E5%AE%89%E5%8D%93%E6%89%8B%E6%9C%BA%E6%8A%93%E5%8C%85/" title="charles 抓包"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="charles 抓包"/></a><div class="content"><a class="title" href="/blog/2024/04/30/android/charles%E5%AE%89%E5%8D%93%E6%89%8B%E6%9C%BA%E6%8A%93%E5%8C%85/" title="charles 抓包">charles 抓包</a><time datetime="2024-04-30T06:49:29.279Z" title="发表于 2024-04-30 14:49:29">2024-04-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/04/26/python/13%E3%80%81playwright/" title="playwright"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="playwright"/></a><div class="content"><a class="title" href="/blog/2024/04/26/python/13%E3%80%81playwright/" title="playwright">playwright</a><time datetime="2024-04-26T07:30:36.069Z" title="发表于 2024-04-26 15:30:36">2024-04-26</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2024 By 侯玉东</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"></div><div class="aplayer no-destroy" data-id="8873950678" data-server="netease" data-type="playlist" data-fixed="true" data-autoplay="true"> </div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-heart.min.js" async="async" mobile="false"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener
  btf.removeGlobalFnEvent('pjax')
  btf.removeGlobalFnEvent('themeChange')

  document.getElementById('rightside').classList.remove('rightside-show')
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()
})

document.addEventListener('pjax:error', e => {
  if (e.request.status === 404) {
    pjax.loadUrl('/blog/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>